package com.hc.server.actions;

import java.io.IOException;
import java.util.Date;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.hc.server.Error;
import com.hc.server.IAction;
import com.hc.server.Request;
import com.hc.server.Response;
import com.hc.server.conn.mall.MallResponse;
import com.hc.server.conn.mall.MallServerHelper;

public class QUERY_AIRLINES implements IAction {

	private final static String FROM_CITY = "FROM_CITY";
	private final static String TO_CITY = "TO_CITY";
	private final static String CHECKIN_DATE = "CHECKIN_DATE";
	private final static String SORT = "SORT";
	private final static String PAGE_INDEX = "PAGE_INDEX";
	private final static String PAGE_SIZE = "PAGE_SIZE";
	
	private final static String STIME = "STIME";
	private final static String ETIME = "ETIME";
	private final static String CD = "CD";	//航班号
	private final static String COM = "COM"; //航空公司
	private final static String MODEL = "MODEL";//机型
	private final static String MPRICE = "MPRICE";
	private final static String SPRICE = "SPRICE";
	private final static String DISCOUNT = "DISCOUNT";//折扣
	private final static String SEAT = "SEAT";//舱位
	private final static String SAIRPORT = "SAIRPORT";
	private final static String EAIRPORT = "EAIRPORT";
	private final static String TGQ = "TGQ";//退改签的说明
	private final static String STOCK = "STOCK";//库存
	private final static String AIRLINES = "AIRLINES";
	private final static String TOTAL_RESULT = "TOTAL_RESULT";
	
	private final static String MALL_TOTAL_COUNT = "totalCount";
//	private final static String MALL_ = "rowsOfPage";
//	private final static String MALL_ = "totalPage";
//	private final static String MALL_ = "currentPage";
	private final static String MALL_LIST = "list";
	
	@Override
	public void execute(Request req, Response resp) {
		//1.获取请求参数
		String fromCity;
		String toCity;
		String checkinDate;
		try {
			fromCity = req.getStrArg(FROM_CITY);
			toCity = req.getStrArg(TO_CITY);
			checkinDate = req.getStrArg(CHECKIN_DATE);
		} catch (JSONException e) {
			resp.setErrResp(Error.INVALID_ARGS_ERR_CD, Error.INVALID_ARGS_ERR_MSG);
			return;
		}
		int orderBy = req.optIntArg(SORT);
		int pageIndex = req.optIntArg(PAGE_INDEX);
		int pageSize = req.optIntArg(PAGE_SIZE);
		//2.查询结果
		MallResponse mResp = null;
		try {
			mResp = MallServerHelper.queryAirlines(fromCity, toCity, checkinDate, orderBy);
		} catch (JSONException e) {
			e.printStackTrace();
			resp.setErrResp(Error.MALLSERVER_DATA_FORMAT_ERR_CD, Error.MALLSERVER_DATA_FORMAT_ERR_MSG);
			return;
		} catch (IOException e) {
			e.printStackTrace();
			resp.setErrResp(Error.MALLSERVER_NET_ERR_CD, Error.MALLSERVER_NET_ERR_MSG);
			return;
		}
		if (0 != mResp.getRetCode()) {
			resp.setErrResp(Error.MALLSERVER_ERR_CD, mResp.getErrorMsg());
			return;
		}
		
		//3.组织返回结果
		try{
			JSONObject result = mResp.getResult();
			JSONArray airlines = new JSONArray();
			JSONArray list = result.getJSONArray(MALL_LIST);//应该使用get方式
			if(0 != list.length()){
				for(int index = 0; index < list.length(); index++){
					JSONObject tmp = list.getJSONObject(index);
					JSONObject airline = new JSONObject();
//					airline.put(CD, value);
//					airline.put(COM, value);
//					airline.put(FROM_CITY, value);
//					airline.put(TO_CITY, value);
//					airline.put(CHECKIN_DATE, value);
//					airline.put(STIME, value);
//					airline.put(ETIME, value);
//					airline.put(SAIRPORT, value);
//					airline.put(EAIRPORT, value);
//					airline.put(MODEL, value);
//					airline.put(MPRICE, value);
//					airline.put(SPRICE, value);
//					airline.put(DISCOUNT, value);
//					airline.put(SEAT, value);
//					airline.put(STOCK, value);
//					airline.put(TGQ, value);
					
					airlines.put(airline);
				}
			}
			resp.appendRespContent(AIRLINES, airlines);
			resp.appendRespContent(TOTAL_RESULT, result.getInt(MALL_TOTAL_COUNT));
		}catch (JSONException e) {
			e.printStackTrace();
			resp.setErrResp(Error.SERVER_SYS_ERR_CD, Error.SERVER_SYS_ERR_MSG);
			return;
		}
	}

}
